package org.aceor.mddal.common.sqlparser.result.object;

import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.base.Strings;

/**
 * Created by lxue on 16/3/23.
 */
public class Table {
    private String name;
    private String alias;

    public Table() {
    }

    public Table(String name) {
        this.name = name;
    }

    public Table(String name, String alias) {
        this.name = name;
        this.alias = alias;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    @Override
    public int hashCode() {
        //name相同或alias相同就能表示equal.此时hashcode很为难,直接统一设成一样的
        return 123456;
    }

    @Override
    public boolean equals(Object obj) {
        if (null == obj) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Table)) {
            return false;
        }
        Table other = (Table) obj;
        //这里为了放宽限度,空表(即name,alias均为null)和其他表相等
        //主要用于应对a.id 和 id 要相等,但是后者没有表或表别名修饰
        if ((null == name && null == alias)
                || (null == other.name && null == other.alias)) {
            return true;
        }
        return Objects.equal(other.name, name) || Objects.equal(other.alias, alias);
    }

    @Override
    public String toString() {
        if (Strings.isNullOrEmpty(alias)) {
            return name;
        } else if(Strings.isNullOrEmpty(name)) {
            return alias;
        } else {
            return Joiner.on("").join(name, ".", alias);
        }
    }
}
